<?php
require_once('libraryfiles/config.php');
$content = 'search.php';
require_once TMP_USER_PATH;
$cat_id = isset($_GET['incategory']) ? $_GET['incategory'] : '';

if ($cat_id != '') {
    $sql2 = "AND pic.category_id IN ($cat_id) ";
} else {
    $sql2 = '';
}

// price between
$priceFrom = isset($_GET['pfrom']) ? $_GET['pfrom'] : '';
$priceTo = isset($_GET['pto']) ? $_GET['pto'] : '';
if ($priceFrom != '' and $priceTo != '') {
    $s_price = 'AND ph.`price` BETWEEN ' . $priceFrom . ' AND ' . $priceTo . '';
} else {
    if ($priceFrom != '') {
        $s_price = 'AND ph.`price` >= ' . $priceFrom . '';
    } else if ($priceTo != '') {
        $s_price = 'AND ph.`price` <= ' . $priceTo . '';
    } else {
        $s_price = '';
    }
}
// price between


$pro_name = isset($_GET['txtsearch']) ? $_GET['txtsearch'] : '';
$pro_name = preg_replace("/<!--.*?-->/", "", $pro_name);
?>
<div class="row">
    <div class="nine columns">
        <div style="margin-right:15px;">
            <div class="block-header">
                <h4>Search Result for Keyword &quot;<?php echo $pro_name; ?>&quot;</h4>
            </div>
<?php
$sql = "SELECT distinct ph.product_id, ph.country_id, p.*, ph.*, pic.* FROM tblproduct AS p, tblproduct_in_pharmacy AS ph, tblproduct_in_category AS pic  WHERE p.product_name LIKE '$pro_name%' $sql2 AND p.status = 1 AND ph.status = 1 AND ph.product_id = p.id $s_price GROUP BY ph.product_id ORDER BY p.product_name";
$result = mysql_query($sql);
if (dbNumRows($result)) {
    $i = 1;
    while ($row = mysql_fetch_assoc($result)) {
        $sql_img = "SELECT thumbnail FROM tblimage WHERE product_id = " . $row['id'] . " LIMIT 0,1";
        $result_img = mysql_query($sql_img);
        $row_img = mysql_fetch_assoc($result_img);
        $i++;
        $pharmacy = mysql_query("SELECT COUNT(DISTINCT pharmacy_id) AS pid
    				FROM tblproduct_in_pharmacy WHERE product_id=" . $row['id'] . "");

        $price = mysql_query("SELECT price, regular_price FROM tblproduct_in_pharmacy WHERE product_id=" . $row['id'] . " ORDER BY price ASC LIMIT 0,1");
        $myprice = mysql_fetch_assoc($price);
        $price1 = mysql_query("SELECT price, regular_price FROM tblproduct_in_pharmacy WHERE product_id=" . $row['id'] . " ORDER BY price DESC LIMIT 0,1");
        $myprice1 = mysql_fetch_assoc($price1);
        ?>
                    <div class="three columns">
                        <div class="product-cat">        
                    <?php
                    if ($row_img['thumbnail'] != "") {
                        $thumbnail_i = $row_img['thumbnail'];
                    } else {
                        $thumbnail_i = 'no-image-small.png';
                    }
                    ?>
                            <div class="image_por">
                                <a href="product/product_detail.php?product_id=<?php echo $row['id'] ?>" title="<?php echo $row['product_name']; ?>"><img src="<?php echo W_S_IMAGES_DIR . $thumbnail_i; ?>" /></a>
                            </div>
                            <div class="pro-title">
                                <a href="product/product_detail.php?product_id=<?php echo $row['id'] ?>" title="<?php echo $row['product_name']; ?>"><?php echo $row['product_name']; ?></a>
                            </div>
                            <div class="add-tocart">
                                <a class="btn btn-mini btn-success" href="libraryfiles/cart-functions.php?action=add&amp;pid=<?php echo $row['id'] ?>">add to cart</a>
                            </div>
                            <div class="row">                   		
                                <span class="price">
                                <?php
//                                        if($myprice['price']==$myprice1['price']){
//                                            echo $myprice['price']."$";
//                                        }
//                                        else{
//                                            echo $myprice['price'] .' - '. $myprice1['price']."$" ; 
//                                        }

                                    if ($myprice['price'] == $myprice['regular_price']) {
                                ?>
                                        <div class="price right"><?php echo $myprice['price']; ?>$</div>
                                        <?php
                                    } else {
                                        ?>
                                        <div class="price right" style="text-decoration: line-through;"><?php echo $myprice['regular_price']; ?>$</div><br/>
                                        <div class="price right"><?php echo $myprice['price']; ?>$</div>
                                        <?php
                                    }
                                    ?>
                                </span>                   	 

                            </div>                 
                        </div>         
                    </div>         



                                    <?php
                                }
                            } else {
                                ?>

                <h3 align="center">No Product Found!</h3>

    <?php
}
?>


        </div>
    </div>



    <div class="three columns">
            <?php
            if (preg_match("/seller/", $_SERVER['PHP_SELF'])) {
                include(S_ROOT . '/seller/left.php');
            } else if (preg_match("/buyer/", $_SERVER['PHP_SELF'])) {
                include(S_ROOT . '/buyer/left.php');
            }
            ?>
<?php
include(S_ROOT . '/includefiles/browser.php');
?> 	


        <?php
        include(S_ROOT . '/includefiles/banner_ads_left.php');
        ?>
    </div>
</div><!--<div class="row">-->